package org.activiti;

import org.activiti.engine.IdentityService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Bootstrap {

    public static void main(String[] args) {
        SpringApplication.run(Bootstrap.class, args);
    }

    @Bean
    InitializingBean usersAndGroupsInitializer(final IdentityService identityService) {

        return new InitializingBean() {
            public void afterPropertiesSet() throws Exception {

                Group group = identityService.newGroup("role1");
                group.setName("role1");
                group.setType("role1");
                identityService.saveGroup(group);

                Group group2 = identityService.newGroup("role2");
                group2.setName("role2");
                group2.setType("role2");
                identityService.saveGroup(group2);

                User user1 = identityService.newUser("user1");
                user1.setPassword("user1");
                identityService.saveUser(user1);

                User user2 = identityService.newUser("user2");
                user2.setPassword("user2");
                identityService.saveUser(user2);

                User user3 = identityService.newUser("user3");
                user3.setPassword("user3");
                identityService.saveUser(user3);

                identityService.createMembership("user1", "role1");
                identityService.createMembership("user2", "role2");
                identityService.createMembership("user3", "role2");
            }
        };
    }

}
